package bishi;

import common.TreeNode;
import utils.TreeNodeUtil;

import java.util.*;

public class Test4 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode();
        root.val = 2;
        root.left = new TreeNode();
        root.right = new TreeNode();
        root.left.val = 1;
        root.right.val = 3;
        System.out.println(new SolutionTest4().getDis(root));
    }
}

class SolutionTest4{
    public int getDis (TreeNode root) {
        Map<Integer,Integer> map = new TreeMap<>();
        List<Integer> list = new ArrayList<>();
        fun(root,0,map,list);
        Collections.sort(list);
        int val1 = list.get(0);
        int val2 = list.get(list.size()-1);
        return map.get(val1) + map.get(val2);
    }

    public int fun2(TreeNode root,int val1,int val2){
        return 0;
    }

    public void fun3(TreeNode root,int val,List<Integer> list){
        if(root == null)    return;
        if(root.val == val){

        }
    }

    public void fun(TreeNode root,int i,Map<Integer,Integer> map,List<Integer> list){
        if(root.left == null && root.right == null){
            map.put(root.val,i);
            list.add(root.val);
            return;
        }
        if(root.left != null){
            fun(root.left,i+1,map,list);
        }
        if(root.right != null){
            fun(root.right,i+1,map,list);
        }
    }
}
